﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;

public partial class admin_addCourseware : System.Web.UI.Page
{
    db_courseware courseware = new db_courseware();
    static DataSet ds = new DataSet();
    String coursewareId="";
    
    protected void Page_Load(object sender, EventArgs e)
    {
    		coursewareId = courseware.getNewCoursewareId();
    		
    		if (coursewareId == "")
    		{
    			Response.Redirect("manageCourseware.aspx");
    		}
    		
        Server.Execute("../inc/checkPurview.aspx");  // 此处用户权限验证

        if (!Page.IsPostBack)
        {
            tb_coursewareId.Text = coursewareId;
            initCourseId();

            img_pic.ImageUrl = "~/upload/courseware/pic/courseware.gif";
            tb_picUrl.Text= "/upload/courseware/pic/courseware.gif";

            string id = Request.QueryString["id"];
            if (!string.IsNullOrEmpty(id))
            {
                ddl_courseId.SelectedValue = Request.QueryString["id"];
            }

        }
    }


    private void initCourseId()
    {
        db_course course = new db_course();
        DataSet ds = new DataSet();

        ds = course.selectCourseALL();

        if (ds.Tables.Count != 0)
        {
            ddl_courseId.DataSource = ds.Tables[0].DefaultView;
            ddl_courseId.DataTextField = "courseName";
            ddl_courseId.DataValueField = "courseId";
            ddl_courseId.DataBind();
            ddl_courseId.Items.Insert(0, new ListItem("请选择课程", ""));
            ddl_courseId.Items[0].Selected = true;
        }
    }
		
	

    protected void btn_ok_Click(object sender, EventArgs e)
    {
        string coursewareUrl = tb_coursewareUrl.Text;
        string about = tb_about.Text;
        string courseId = ddl_courseId.SelectedValue;
        string picUrl = tb_picUrl.Text;
        string coursewareName = tb_coursewareName.Text;
       // string teacherId = ddl_teacherId.SelectedValue;
        string teacherId = new db_course().selectCourseONE(courseId).Tables[0].Rows[0]["teacherId"].ToString();

        if (courseware.addCourseware(coursewareId,about,courseId,coursewareUrl,picUrl,teacherId,coursewareName))
        {
            Response.Write("<script language=javascript>alert('新增课件信息成功！');location.href='manageCourseware.aspx';</script>");
        }
        else
        {
            Response.Write("<script language=javascript>alert('失败！');</script>");
        }
    }
    protected void btn_res_Click(object sender, EventArgs e)
    {
          tb_coursewareUrl.Text = "";
          tb_about.Text = "";
         // ddl_courseId.Items[0].Selected = true;
          tb_picUrl.Text = "";
         // ddl_teacherId.Items[0].Selected = true;
    }
    
	  
   protected void btn_uploadCourseware_Click(object sender, EventArgs e)
   {
       if (tb_coursewareName.Text.Trim().Equals(""))
       {
           Response.Write("<script language=javascript>alert('请填写课件名称！');</script>");
           return;
       }

        try
        {
            if (fud_courseware.PostedFile.FileName == "")
            {
                Response.Write("<script language=javascript>alert('请选择上传文件！');</script>");
            }
            else
            {
                string filepath = fud_courseware.PostedFile.FileName;  //得到的是文件的完整路径,包括文件名，如：C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg 
                if (Tools.IsAllowedExtension_course(fud_courseware))
                {
                    string fileExt = filepath.Substring(filepath.LastIndexOf("."));//.jpg 
                    String newFileName = coursewareId + tb_coursewareName.Text + fileExt; //DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
                    string serverpath = Server.MapPath("~/upload/courseware/") + newFileName;//取得文件在服务器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg 
                    fud_courseware.PostedFile.SaveAs(serverpath);//将上传的文件另存为
                    tb_coursewareUrl.Text = "/upload/courseware/" + newFileName;
                    Response.Write("<script language=javascript>alert('上传成功！');</script>");
                }
                else
                {
                    Response.Write("<script language=javascript>alert('上传文件格式不正确！必须为：ppt,pptx,doc,docx,rar,zip格式');</script>");
                }
            }

        }
        catch
        {
            Response.Write("<script language=javascript>alert('上传发生错误！');</script>");
        }
   
   }
    
    
    
    
    protected void btn_uploadPic_Click(object sender, EventArgs e)
    {
        try
        {
            if (fud_pic.PostedFile.FileName == "")
            {
                Response.Write("<script language=javascript>alert('请选择上传文件！');</script>");
            }
            else
            {
                string filepath = fud_pic.PostedFile.FileName;  //得到的是文件的完整路径,包括文件名，如：C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg 
                if (Tools.IsAllowedExtension(fud_pic))
                {
                    if (fud_pic.FileContent.Length / 1024 < 200)//检查图片大小
                    {
                        string fileExt = filepath.Substring(filepath.LastIndexOf("."));//.jpg 
                        String newFileName =coursewareId + fileExt; //DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
                        string serverpath = Server.MapPath("~/upload/courseware/pic/") + newFileName;//取得文件在服务器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg 
                        fud_pic.PostedFile.SaveAs(serverpath);//将上传的文件另存为 
                        tb_picUrl.Text = "/upload/courseware/pic/" + newFileName;
                        img_pic.ImageUrl = "~/upload/courseware/pic/" + newFileName;
                        Response.Write("<script language=javascript>alert('上传成功！');</script>");
                    }
                    else
                    {
                        Response.Write("<script language=javascript>alert('上传图片文件过大，请上传小于200K的图片文件');</script>");
                    }
                }
                else
                {
                    Response.Write("<script language=javascript>alert('上传文件格式不正确！必须为：jpg,gif,bmp,png格式');</script>");
                }
            }
        }
        catch
        {
            Response.Write("<script language=javascript>alert('上传发生错误！');</script>");
        }
    }





}